#!/bin/bash
# 直接通过远程curl的方式运行（像Homebrew一样）

# rsync是为了传输pack文件
# sqlite是为了server能顺利运行（如果以后用postgresql，就可以删掉）
yum install -y git rsync sqlite-devel

### /opt

# 将git脚本仓库下载，并将它移到opt，然后将path添加到bashrc中
git clone git@gitee.com:LucidQuantum/lolo.git
mv lolo /opt
echo 'PATH=$PATH:/opt/lolo/linux' >> /etc/bashrc

# 安装dart SDK（2.18.6）
wget https://storage.googleapis.com/dart-archive/channels/stable/release/2.18.6/sdk/dartsdk-linux-x64-release.zip
unzip dartsdk-linux-x64-release.zip -d /opt
rm -f dartsdk-linux-x64-release.zip
echo 'PATH=$PATH:/opt/dart-sdk/bin' >> /etc/bashrc

# 安装dhttpd
/opt/dart-sdk/bin/dart pub global activate dhttpd
echo 'PATH=$PATH:~/.pub-cache/bin' >> /etc/bashrc

### /srv

# 下载dart工具包和server
git clone git@gitee.com:LucidQuantum/dart_tools.git
cd dart_tools
/opt/dart-sdk/bin/dart pub get
cd ..

git clone git@gitee.com:LucidQuantum/bitotus_server.git
cd bitotus_server
/opt/dart-sdk/bin/dart pub get
cd ..

# 将两者移到/srv文件夹中
mv dart_tools /srv
mv bitotus_server /srv

# 后台运行服务器（必须要进去启动，不然db会创建在~/）
cd /srv/bitotus_server
nohup /opt/dart-sdk/bin/dart main.dart &
sleep 5
pid=$(netstat -lnp | grep 0.0.0.0:1209 | awk '{print $7}' | awk -F/ '{print $1}')
if [ -n $pid ]
then
    echo "server已启动，pid为$pid"
else
    echo "server启动失败，pid为$pid"
fi

cd
exec bash